package com.infore.siteManagement.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.infore.siteManagement.entity.Area;

public class AreaDiguiUtil {
    private List<Area> rootList; //根节点对象存放到这里

    private List<Area> bodyList; //其他节点存放到这里，可以包含根节点
    
    private List<Area> parentList;

    public AreaDiguiUtil(List<Area> rootList, List<Area> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public List<Area> getParents(){   //调用的方法入口
        parentList = new ArrayList<Area>();
        if(bodyList != null && !bodyList.isEmpty()){
        //声明一个map，用来过滤已操作过的数据
            Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getParents(beanTree,map));
            return parentList;
        }
        return parentList;
    }

    public void getParents(Area beanTree,Map<String,String> map){
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getAreaId()))
                .filter(c -> (c.getAreaId()).equals(beanTree.getParentId()))
                .forEach(c ->{
                    map.put(c.getAreaId(),c.getParentId());
                    getParents(c,map);
                    parentList.add(c);
                });
    }

}

